#!/bin/bash

# Build docker files

# Paths
CUR_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
PROJECT_ROOT="$(realpath "${CUR_DIR}/..")"
OPERATOR_DOCKERFILE_TEMPLATE="${PROJECT_ROOT}/dockerfile/operator/template/Dockerfile"
OPERATOR_DOCKERFILE="${PROJECT_ROOT}/dockerfile/operator/Dockerfile"
METRICS_EXPORTER_DOCKERFILE_TEMPLATE="${PROJECT_ROOT}/dockerfile/metrics-exporter/template/Dockerfile"
METRICS_EXPORTER_DOCKERFILE="${PROJECT_ROOT}/dockerfile/metrics-exporter/Dockerfile"

# Versions
VERSION=$(cat ${PROJECT_ROOT}/release)
RELEASE="1"
OPERATOR_VERSION="${OPERATOR_VERSION:-$VERSION}"
METRICS_EXPORTER_VERSION="${METRICS_EXPORTER_VERSION:-$VERSION}"

# Run generator

#
# Build dockerfiles
#

cat "${OPERATOR_DOCKERFILE_TEMPLATE}" | \
  VERSION="${VERSION}" \
  RELEASE="${RELEASE}" \
  OPERATOR_VERSION="${OPERATOR_VERSION}" \
  METRICS_EXPORTER_VERSION="${METRICS_EXPORTER_VERSION}" \
  envsubst \
  > "${OPERATOR_DOCKERFILE}"

cat "${METRICS_EXPORTER_DOCKERFILE_TEMPLATE}" | \
  VERSION="${VERSION}" \
  RELEASE="${RELEASE}" \
  OPERATOR_VERSION="${OPERATOR_VERSION}" \
  METRICS_EXPORTER_VERSION="${METRICS_EXPORTER_VERSION}" \
  envsubst \
  > "${METRICS_EXPORTER_DOCKERFILE}"
